package com.gemme.gemme.shequ;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gemme.gemme.MainActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.fabu.FabuFragment;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.wode.ShequPsonDetActivity;

import org.json.JSONObject;

import java.util.HashMap;

public class GuanzhuSingleView extends RelativeLayout {
    private Context context;
    private SheQUTuijianBean.DataBean dataBean;

    public GuanzhuSingleView(Context context) {
        this(context,null);
    }

    public GuanzhuSingleView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public GuanzhuSingleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }
    ImageView iv_head;
    ImageView iv_delete;
    TextView tv_name;
    TextView tv_type;
    TextView tv_guanzhu;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.guanzhu_single_view,this);

        iv_head = findViewById(R.id.iv_head);
        iv_delete = findViewById(R.id.iv_delete);
        tv_name = findViewById(R.id.tv_name);
        tv_type = findViewById(R.id.tv_type);
        tv_guanzhu = findViewById(R.id.tv_guanzhu);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
//                context.startActivity(new Intent(context,ShequPersonActivity.class));
                //进入社区个人中心页
                if(TextUtils.isEmpty(dataBean.getMobile())){
                    Toast.makeText(context, "对方暂无手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(context, ShequPsonDetActivity.class);
                intent.putExtra("data",dataBean.getId());
                context.startActivity(intent);
            }
        });

        tv_guanzhu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                toGuanzhu();
            }
        });

    }
/*    iv_head
            iv_delete
    tv_name
    tv_type
    tv_guanzhu*/
    public void setData(SheQUTuijianBean.DataBean dataBean) {
        this.dataBean = dataBean;
        try {
            tv_name.setText(dataBean.getNickname());

            Glide.with(context).load(StrUtil.noNUll(dataBean.getHeadImgUrl())).transform(new GlideCircleTransform(context))
                    .into(iv_head);

        }catch (Exception e){

        }
    }

    private void toGuanzhu() {
        try {
            int userId = dataBean.getId();
            String url = DataManager.BaseUrl + "community/appcusersfans/focuson?userId=" + userId;

            HashMap<String, String> headMap = new HashMap<>();
            headMap.put(DataManager.AuthorizationKey,DataManager.getToken(context));
            DataManager.toServer2(context, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
                @Override
                public void onResponse(String response) {
                    try {

//                        getDataS();
                        String msg = (new JSONObject(response)).getString("msg");
                        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();

                        if(context instanceof MainActivity){
                            MainActivity act = (MainActivity) GuanzhuSingleView.this.context;
                            ShequFragment fabuFragment = act.getFabuFragment();
                            fabuFragment.onLazyLoad();
                        }
                    }catch (Exception e){

                    }
                    System.out.println("");
                }
            });
        }catch (Exception e){

        }

    }

}
